package middleware

import (
	"strconv"

	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
)

func AuthMiddleware() http.Middleware {
	return func(ctx http.Context) {

		token := ctx.Request().Header("Authorization")
		parse, err := facades.Auth(ctx).Parse(token)
		if err != nil {
			ctx.Request().AbortWithStatusJson(http.StatusOK, http.Json{
				"code":    http.StatusUnauthorized,
				"message": err.Error(),
			})
		}
		atoi, _ := strconv.Atoi(parse.Key)

		ctx.WithValue("adminId", atoi)
		ctx.Request().Next()
	}
}
